package com.yingda.electronicscaleadvices.manege;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.yingda.electronicscaleadvices.dialog.LoadDialog;
import com.yingda.electronicscaleadvices.utils.BaseHandler;
import java.util.ArrayList;
import java.util.List;

import butterknife.Unbinder;


public abstract class BaseActivity extends AppCompatActivity {

    private static final String TAG = "BaseActivity";
    protected static boolean isShow = false;
    public static   List<Activity> activities = new ArrayList<Activity>();
    //判断人脸识别是不是已经初始化
    protected volatile boolean initFaceComplete = false;

    protected Unbinder bind;
    protected LoadDialog mDialog;
    protected BaseHandler mHandler = new BaseHandler(this, new BaseHandler.HandleMsgCallback() {
        @Override
        public void handleMessage(Message msg) {
            handleActMessage(msg);
        }
    });

    protected void handleActMessage(Message msg) {

    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //保持屏幕常亮，也可以再布局文件顶层：android:keepScreenOn="true"
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        AppManager.getAppManager().addActivity(this);
        //去除标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //获取权限
        requestPerm();
    }
    /**
     * 添加Activity
     * @param activity 添加的Activity对象
     * */
    public  void addActivity(Activity activity) {
        activities.add(activity);
    }

    /**
     * 删除Activity
     * @param activity 删除的Activity对象
     * */
    public  void removeActivity(Activity activity) {
        activities.remove(activity);
    }

    private void requestPerm() {
        XXPermissions.with(this)
                //单个权限
                .permission(Permission.WRITE_EXTERNAL_STORAGE)
                .permission(Permission.READ_EXTERNAL_STORAGE)
                .permission(Permission.CAMERA)
                .permission(Permission.SYSTEM_ALERT_WINDOW)
                // .interceptor(new IPermissionInterceptor() {})
                //  .unchecked()
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            //  ToastUtils.show("权限所有的获取");

                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            Log.e(TAG, "onDenied：被永久拒绝授权，请手动授予权限 ");
                            XXPermissions.startPermissionActivity(BaseActivity.this, permissions);
                        } else {
                            Log.e(TAG, "onDenied: 权限获取失败");
                        }
                    }
                });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == XXPermissions.REQUEST_CODE) {
            if (XXPermissions.isGranted(this, Permission.ACCESS_FINE_LOCATION) &&
                    XXPermissions.isGranted(this, Permission.ACCESS_COARSE_LOCATION)) {
                ToastUtils.show("权限已获取");

            } else {
                ToastUtils.show("权限获取失败");
            }
        }
    }
    protected void showWaitingDlg() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
        mDialog = new LoadDialog(this);
        mDialog.show();
    }

    protected void dismissWaitingDlg() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bind != null) {
            bind.unbind();
        }
        removeActivity(this);
    }


    /**
     * 关闭所有Activity
     * */
    public static   void finishAll() {
        for (Activity activity : activities) {
            if (activity!=null) {
                activity.finish();
            }
        }
        activities.clear();
    }

}
